package spring.cloud.tasks.client.batch_task_processor;

import lombok.Getter;
import lombok.Setter;
import spring.cloud.tasks.client.BatchTaskWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class Tasker {
    @Setter
    @Getter
    private boolean needStop = false;//自己主动检查需要将自己的线程终止
    @Setter
    @Getter
    private boolean stop = false;//外部指令将自己的线程终止

    //
    public String pingpongOrReportStatus(BatchTaskWorker batchTaskWorker, TaskInstanceExecuterThreadInfo taskInstanceExecuterThreadInfo) {
        Map<String, TaskInfo> taskIdToBatchTaskInfoMap = batchTaskWorker.getTaskIdToBatchTaskInfoMap();
        if (taskIdToBatchTaskInfoMap == null) {
            setNeedStop(true);
            return null;
        }
        TaskInfo taskInfo = taskIdToBatchTaskInfoMap.get(taskInstanceExecuterThreadInfo.getTaskId());
        if (taskInfo == null) {
            setNeedStop(true);
            return null;
        }
        if (taskInfo.isStop()) {
            setStop(true);//外部指令关闭
            return null;
        }
        TaskWorkerInstanceContext taskWorkerInstanceContext = taskInfo.taskInstanceIdToWorkerTaskBatchInfoMap.get(taskInstanceExecuterThreadInfo.getThreadId());
        if (taskWorkerInstanceContext == null) {
            setNeedStop(true);
            return null;
        }
        TaskWorkerInstanceClientInfo taskWorkerInstanceClientInfo = taskWorkerInstanceContext.getTaskWorkerInstanceClientInfo();
        if (taskWorkerInstanceClientInfo == null) {
            setNeedStop(true);
            return null;
        }
        if (TaskCreater.class.getName().equals(taskInstanceExecuterThreadInfo.getThreadType())) {
            TaskWorkerInstanceClientInfo.TaskThreadInfo taskThreadInfo = taskWorkerInstanceClientInfo.getTaskCreaterThreadInfo();
            if (taskThreadInfo == null) {
                setNeedStop(true);
                return null;
            }
            if (taskThreadInfo.getThreadId().equals(taskInstanceExecuterThreadInfo.getThreadId())) {
                taskThreadInfo.setLastUpdateDate(new Date());
                taskThreadInfo.setStatus(taskInstanceExecuterThreadInfo.getStatus());
                return null;
            }
            setNeedStop(true);
            return null;
        } else if (TaskExecuter.class.getName().equals(taskInstanceExecuterThreadInfo.getThreadType())) {
            List<TaskWorkerInstanceClientInfo.TaskThreadInfo> taskThreadInfoList = taskWorkerInstanceClientInfo.getTaskExecuterThreadInfoList();
            for (TaskWorkerInstanceClientInfo.TaskThreadInfo taskThreadInfo : taskThreadInfoList) {
                if (taskThreadInfo != null && taskThreadInfo.getThreadId().equals(taskInstanceExecuterThreadInfo.getThreadId())) {
                    taskThreadInfo.setLastUpdateDate(new Date());
                    taskThreadInfo.setStatus(taskInstanceExecuterThreadInfo.getStatus());
                    return null;
                }
            }
            setNeedStop(true);
            return null;
        } else if (TaskResultCollecter.class.getName().equals(taskInstanceExecuterThreadInfo.getThreadType())) {
            TaskWorkerInstanceClientInfo.TaskThreadInfo taskThreadInfo = taskWorkerInstanceClientInfo.getTaskResultCollecterThreadInfo();
            if (taskThreadInfo == null) {
                setNeedStop(true);
                return null;
            }
            if (taskThreadInfo.getThreadId().equals(taskInstanceExecuterThreadInfo.getThreadId())) {
                taskThreadInfo.setLastUpdateDate(new Date());
                taskThreadInfo.setStatus(taskInstanceExecuterThreadInfo.getStatus());
                return null;
            }
            setNeedStop(true);
            return null;

        } else {
            setNeedStop(true);
            return null;
        }

    }
}
